package com.xcity.game.buildv2.template;

import com.xcity.game.battle.pve.template.StageTemplate;
import com.xcity.game.item.DropGroupTemplate;
import com.xcity.game.item.ItemTemplate;

import naga.x.App;
import naga.x.game.template.Template;
import naga.x.game.template.TemplateException;
import naga.x.game.template.TemplateService;
import naga.x.game.template.annotation.Binding;
import naga.x.game.template.annotation.Excel;

/**
 * 建筑v2
 * @author yang.li
 *
 */
@Excel(value = "build.xlsx", sheet = 2)
public class BuildTemplateV2 extends Template {

	/** 建筑名 */
	protected String name;

	/** 建筑图片 */
	protected String image;

	/** 价值 */
	protected int worth;

	/** 物品数量 */
	protected int part1ItemNum;

	/** 该部件需求现金 */
	protected int part1Gold;

	/** 物品递增量 */
	protected int part1ItemDelta;

	/** 现金递增量 */
	protected int part1GoldDelta;

	/** 物品数量 */
	protected int part2ItemNum;

	/** 该部件需求现金 */
	protected int part2Gold;

	/** 物品递增量 */
	protected int part2ItemDelta;

	/** 现金递增量 */
	protected int part2GoldDelta;

	/** 物品数量 */
	protected int part3ItemNum;

	/** 该部件需求现金 */
	protected int part3Gold;

	/** 物品递增量 */
	protected int part3ItemDelta;

	/** 现金递增量 */
	protected int part3GoldDelta;

	/** 物品数量 */
	protected int part4ItemNum;

	/** 该部件需求现金 */
	protected int part4Gold;

	/** 物品递增量 */
	protected int part4ItemDelta;

	/** 现金递增量 */
	protected int part4GoldDelta;

	/** 物品数量 */
	protected int part5ItemNum;

	/** 该部件需求现金 */
	protected int part5Gold;

	/** 物品递增量 */
	protected int part5ItemDelta;

	/** 现金递增量 */
	protected int part5GoldDelta;
	
	/** 建筑产钱系数百分点 */
	protected int produceMoneyFactor;
	
	/** 每个部位每升一级产出经验 */
	protected int partExp;
	
	/** 每个部位建造完成后奖励掉落 */
	protected int partDoneDropId;
	protected DropGroupTemplate partDoneDrop;
	
	/** 解锁该建筑需通关的关卡 */
	protected StageTemplate unlockStage;

	public static class PartTemplate {
		public int itemId; // 物品id
		public int count; // 需求数量
		public int itemDelta; // 物品递增量
		private ItemTemplate item;
		public int gold; // 需求现金
		public int goldDelta; // 现金递增量
		public String name; // 部位名
		public PartTemplate(int itemId, int count, int itemDelta, int gold, int goldDelta, String name) {
			this.itemId = itemId;
			this.count = count;
			this.itemDelta = itemDelta;
			this.gold = gold;
			this.goldDelta = goldDelta;
			this.name = name;
		}
		public ItemTemplate getItem() {
			return item != null ? item : (item = App.getApp().getServiceManager().get(TemplateService.class).get(ItemTemplate.class, itemId));
		}
		public int getItemCount(int level) {
			return count + --level * itemDelta;
		}
		public int getGold(int level) {
			return gold + --level * goldDelta;
		}
	}
	public static final int PART3_ITEM_ID = 30051; // 木材
	public static final int PART5_ITEM_ID = 30052; // 钢筋
	public static final int PART2_ITEM_ID = 30048; // 水泥
	public static final int PART1_ITEM_ID = 30049; // 红砖
	public static final int PART4_ITEM_ID = 30050; // 沙土
	protected PartTemplate[] parts;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getImage() {
		return image;
	}

	public void setImage(String image) {
		this.image = image;
	}

	public int getWorth() {
		return worth;
	}

	public void setWorth(int worth) {
		this.worth = worth;
	}

	public int getPart1ItemNum() {
		return part1ItemNum;
	}

	public void setPart1ItemNum(int part1ItemNum) {
		this.part1ItemNum = part1ItemNum;
	}

	public int getPart1Gold() {
		return part1Gold;
	}

	public void setPart1Gold(int part1Gold) {
		this.part1Gold = part1Gold;
	}

	public int getPart1ItemDelta() {
		return part1ItemDelta;
	}

	public void setPart1ItemDelta(int part1ItemDelta) {
		this.part1ItemDelta = part1ItemDelta;
	}

	public int getPart1GoldDelta() {
		return part1GoldDelta;
	}

	public void setPart1GoldDelta(int part1GoldDelta) {
		this.part1GoldDelta = part1GoldDelta;
	}

	public int getPart2ItemNum() {
		return part2ItemNum;
	}

	public void setPart2ItemNum(int part2ItemNum) {
		this.part2ItemNum = part2ItemNum;
	}

	public int getPart2Gold() {
		return part2Gold;
	}

	public void setPart2Gold(int part2Gold) {
		this.part2Gold = part2Gold;
	}

	public int getPart2ItemDelta() {
		return part2ItemDelta;
	}

	public void setPart2ItemDelta(int part2ItemDelta) {
		this.part2ItemDelta = part2ItemDelta;
	}

	public int getPart2GoldDelta() {
		return part2GoldDelta;
	}

	public void setPart2GoldDelta(int part2GoldDelta) {
		this.part2GoldDelta = part2GoldDelta;
	}

	public int getPart3ItemNum() {
		return part3ItemNum;
	}

	public void setPart3ItemNum(int part3ItemNum) {
		this.part3ItemNum = part3ItemNum;
	}

	public int getPart3Gold() {
		return part3Gold;
	}

	public void setPart3Gold(int part3Gold) {
		this.part3Gold = part3Gold;
	}

	public int getPart3ItemDelta() {
		return part3ItemDelta;
	}

	public void setPart3ItemDelta(int part3ItemDelta) {
		this.part3ItemDelta = part3ItemDelta;
	}

	public int getPart3GoldDelta() {
		return part3GoldDelta;
	}

	public void setPart3GoldDelta(int part3GoldDelta) {
		this.part3GoldDelta = part3GoldDelta;
	}

	public int getPart4ItemNum() {
		return part4ItemNum;
	}

	public void setPart4ItemNum(int part4ItemNum) {
		this.part4ItemNum = part4ItemNum;
	}

	public int getPart4Gold() {
		return part4Gold;
	}

	public void setPart4Gold(int part4Gold) {
		this.part4Gold = part4Gold;
	}

	public int getPart4ItemDelta() {
		return part4ItemDelta;
	}

	public void setPart4ItemDelta(int part4ItemDelta) {
		this.part4ItemDelta = part4ItemDelta;
	}

	public int getPart4GoldDelta() {
		return part4GoldDelta;
	}

	public void setPart4GoldDelta(int part4GoldDelta) {
		this.part4GoldDelta = part4GoldDelta;
	}

	public int getPart5ItemNum() {
		return part5ItemNum;
	}

	public void setPart5ItemNum(int part5ItemNum) {
		this.part5ItemNum = part5ItemNum;
	}

	public int getPart5Gold() {
		return part5Gold;
	}

	public void setPart5Gold(int part5Gold) {
		this.part5Gold = part5Gold;
	}

	public int getPart5ItemDelta() {
		return part5ItemDelta;
	}

	public void setPart5ItemDelta(int part5ItemDelta) {
		this.part5ItemDelta = part5ItemDelta;
	}

	public int getPart5GoldDelta() {
		return part5GoldDelta;
	}

	public void setPart5GoldDelta(int part5GoldDelta) {
		this.part5GoldDelta = part5GoldDelta;
	}

	public PartTemplate getPart(int pos) {
		return parts[pos];
	}

	public int getProduceMoneyFactor() {
		return produceMoneyFactor;
	}

	public void setProduceMoneyFactor(int produceMoneyFactor) {
		this.produceMoneyFactor = produceMoneyFactor;
	}

	public int getPartExp() {
		return partExp;
	}

	public void setPartExp(int partExp) {
		this.partExp = partExp;
	}

	public int getPartDoneDropId() {
		return partDoneDropId;
	}

	public void setPartDoneDropId(int partDoneDropId) {
		this.partDoneDropId = partDoneDropId;
	}
	
	public DropGroupTemplate getPartDoneDrop() {
		return partDoneDropId == 0 || partDoneDrop != null ? partDoneDrop : (partDoneDrop = App.getApp().getServiceManager().get(TemplateService.class).get(DropGroupTemplate.class, partDoneDropId));
	}
	
	public StageTemplate getUnlockStage() {
		return unlockStage;
	}

	@Binding(type = StageTemplate.class, field = "unlockStageId", ref = "id")
	public void setUnlockStage(StageTemplate unlockStage) {
		this.unlockStage = unlockStage;
	}

	@Override
	public void validate() throws TemplateException {
//		3   5    2   1   4
//		木材 钢筋 水泥 红砖 沙土
//		门窗 立柱 屋顶 墙壁 地面
		parts = new PartTemplate[] {
				new PartTemplate(PART3_ITEM_ID, part3ItemNum, part3ItemDelta, part3Gold, part3GoldDelta, "门窗"),
				new PartTemplate(PART5_ITEM_ID, part5ItemNum, part5ItemDelta, part5Gold, part5GoldDelta, "立柱"),
				new PartTemplate(PART2_ITEM_ID, part2ItemNum, part2ItemDelta, part2Gold, part2GoldDelta, "屋顶"),
				new PartTemplate(PART1_ITEM_ID, part1ItemNum, part1ItemDelta, part1Gold, part1GoldDelta, "墙壁"),
				new PartTemplate(PART4_ITEM_ID, part4ItemNum, part4ItemDelta, part4Gold, part4GoldDelta, "地面"),
		};
	}
}
